<template>
  <!-- 日电量详情 -->
  <div class="-content">
    <div class="search-content">
      <Col class="panel content-header" :span="24">
      <div class="title-bar cf">
        <h5 class="fl">
          <span>
            <a href="javascript:void(0);" class="link" @click="backToLast">
              <i class="fa fa-angle-left" aria-hidden="true"></i>返回上一页</a>
            <span class="dashboard-card-sep">|</span>
            <span class="dashboard-card-name"><span>日电量详情</span></span>
          </span>
        </h5>
      </div>
      </Col>
    </div>
    <div>
      <Row>
        <Col span="24" v-if="showTimeRange" style="padding:10px;box-sizing:border-box;">
        <DatePicker :clearable="false" :options="options" v-model="timeDate" type="date" placeholder="请选择查询时间..." style="width: 220px" @on-change="onTimeChange"></DatePicker>
        </Col>
        <!-- 折线图 -->
        <Col span="24" class="panel map-panel" style="margin-bottom:15px;">
        <div style="width:100%;height:350px;" class="blank-chart">
          <div ref="chart" style="width: 100%;height: 100%;"></div>
        </div>
        </Col>
        <!-- 表格 -->
        <Col class="panel table-panel" :span="24" style="overflow-x: auto;margin-bottom: 20px;">
        <table class="mailTable">
          <tr>
            <td>总电量（kWh）</td>
            <td colspan="3">{{tableData.totalValue}}</td>
          </tr>
          <tr>
            <td>峰电量（kWh）</td>
            <td>{{tableData.peakValue}}</td>
            <td>平电量（kWh）</td>
            <td>{{tableData.flatValue}}</td>
          </tr>
          <tr>
            <td>谷电量（kWh）</td>
            <td>{{tableData.valleyValue}}</td>
            <td>尖电量（kWh）</td>
            <td>{{tableData.sharpValue}}</td>
          </tr>
        </table>
        </Col>
      </Row>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex"
import { getNxPowerDetail, getIdByDeviceNo, getDeviceDetail } from "@/api"
import mixin from "./mixin"
export default {
  mixins: [mixin],
  computed: { ...mapGetters(["color"]) },
  created() {
    const path = this.$route.path
    if (path.indexOf("ontime") > -1) {
      this.showTimeRange = false
    }
  },
  mounted() {
    this.initData()
  },
  data() {
    const vm = this
    return {
      tableData: {},
      deviceName: null,
      echartInstence: null,
      showTimeRange: true
    }
  },
  methods: {
    onTimeChange() {
      this.initData()
    },
    initData() {
      const { id, name } = this.$route.params
      const vm = this
      const timeDate = vm.timeDate,
        date = vm.moment(timeDate).format("YYYY-MM-DD"),
        deviceNo = id,
        params = { date, deviceNo },
        options = {
          title: {
            text: name,
            left: "center",
            subtext: date
          },
          color: ["#3398DB"],
          legend: { bottom: 0 },
          tooltip: {
            trigger: "axis",
            axisPointer: {
              type: "shadow"
            },
            formatter(params) {
              return `${params[0].axisValue} ${params[0].seriesName}<br/>${
                params[0].marker
              } ${params[0].data}kWh `
            }
          },
          grid: {
            top: "20%",
            left: "3%",
            right: "4%",
            bottom: "50"
          },
          xAxis: {
            type: "category",
            data: [],
            axisTick: {
              alignWithLabel: true
            }
          },
          yAxis: [
            {
              name: `单位(kWh)`,
              type: "value"
            }
          ],
          series: [
            {
              name: "电量",
              type: "bar",
              data: []
            }
          ]
        }
      vm.deviceName = name

      vm.$nextTick(() => {
        const dom = vm.$refs.chart
        if (vm.echartInstence) {
          vm.echartInstence.clear()
        }
        vm.echartInstence = vm.echarts.init(dom)
        vm.echartInstence.showLoading({ text: "", color: "red" })
        getNxPowerDetail(params).then(res => {
          const { code } = res
          if (code === 200) {
            const {
              data: { list, fpgj, hourList }
            } = res
            options.xAxis.data = hourList.map(
              t => `${vm.moment(t).format("YYYY-MM-DD HH")}时`
            )
            options.series[0].data = list
            vm.tableData = fpgj
            vm.echartInstence.setOption(options)
            vm.echartInstence.hideLoading()
          } else {
            vm.echartInstence.setOption({
              title: {
                text: "暂无数据",
                textStyle: {
                  fontSize: 28,
                  fontWeight: "normal"
                },
                left: "center",
                top: "center"
              }
            })
            vm.echartInstence.hideLoading()
            vm.tableData = {
              totalValue: "-",
              peakValue: "-",
              flatValue: "-",
              valleyValue: "-",
              sharpValue: "-"
            }
          }
        })
      })
    },

    backToLast() {
      this.$router.go(-1)
    }
  }
}
</script>
